<?php $this->set('title', 'Edit Survey'); ?>

<?php echo $html->link('Surveys', array('controller'=>'Surveys', 'action'=>'index')); ?>

<div class="header">Edit Survey</div>

<!-- Add question link -->
<?php echo $form->create('Survey', array('action' => 'add_question')); ?>
<?php echo $form->hidden('Survey.IdQ', array('value' => $survey_id)); ?>
<?php echo $form->end('Add Question'); ?>

<!-- Add answer link -->
<?php echo $form->create('Survey', array('action' => 'add_answer')); ?>
<div style="padding: 5px 0px 5px 0px;">
<div style="float:left; padding-top:3px;">
Add answer to question: <?php echo $form->select('Question.id', $questionDropDown, null, null, false); ?>
</div>
<div style="float:left; padding: 0px 0px 0px 10px;">
<?php echo $form->hidden('Survey.IdA', array('value' => $survey_id)); ?>
<?php echo $form->end('Add'); ?>
</div>
<div class="clear">&nbsp;</div>
</div>
<br />

<div style="border-top: solid 1px #777777; width:380px; padding:0px 0px 0px 20px;">&nbsp;</div>

<!-- Survey Edit Form -->
<?php echo $form->create('Survey', array('action' => 'edit', 'name' => 'SurveyEditForm')); ?>
<table border="0" cellpadding=="0" cellspacing="0">
	<tr>
		<td class="leftcell">
			Survey Title:
		</td>				
		<td class="rightcell">
			<?php echo($form->error('Survey.name', array('class' => 'errMsg'))); ?>
			<?php echo($form->text('Survey.name', array('style' => 'width:200px;'))); ?>
			<?php echo($form->hidden('Survey.id')); ?>
		</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td class="leftcell" colspan="3">
			<span style="font-size:20px">Questions</span>
		</td>
	</tr>
	
	<!-- Loop through the number of questions -->
	<?php for($q = 0; $q < count($questions); $q++) { ?>
	<tr>
		<td class="leftcell" style="padding-left:25px;">
			Question <?php echo $q+1 ?>:
		</td>				
		<td class="rightcell">
			<?php echo($form->error('Question.'.$q.'.text', array('class' => 'errMsg'))); ?>
			<?php echo($form->textarea('Question.'.$q.'.text', array('style' => 'width:200px;height:50px;',
			 'value' => $questions[$q]['Question']['text']))); ?>
			<?php echo($form->hidden('Question.'.$q.'.id', array('value' => $questions[$q]['Question']['id']))); ?>
		</td>
		<td>
			<!-- Move Up -->
			<?php echo $html->link($html->image("icons/arrow_large_up.png"),
		 	  array('controller' => 'Questions', 'action' => 'moveUp', $questions[$q]['Question']['id'], $survey_id, $questions[$q]['Question']['viewOrder']),
		 	  array('escape' => false))?>
			<!-- Move Down -->
			<?php echo $html->link($html->image("icons/arrow_large_down.png"),
		 	  array('controller' => 'Questions', 'action' => 'moveDown', $questions[$q]['Question']['id'], $survey_id, $questions[$q]['Question']['viewOrder']),
		 	  array('escape' => false))?>&nbsp;
			  <!-- Delete -->
		  	<?php if($q != 0) { ?>
			<?php echo $html->link($html->image("icons/remove.png"),
			  array('controller'=>'Questions', 'action'=>'delete', $questions[$q]['Question']['id'], $survey_id), null,
			  'Are you sure you want to delete this question?',
			  array('escape' => false)); ?>
		  	<?php } ?>
		</td>
	</tr>
	<!-- Loop through the number of answers for this question -->
	<?php for($a = 0; $a < count($answers[$q]); $a++) { ?>
	<tr>
		<td class="leftcell" style="padding-left:35px;">
			Answer <?php echo $a+1 ?>:
		</td>				
		<td class="rightcell">
			<?php echo($form->error('Answer.'.$q.'.'.$a.'.text', array('class' => 'errMsg'))); ?>
			<?php echo($form->textarea('Answer.'.$q.'.'.$a.'.text', array('style' => 'width:200px;height:50px;',
			 'value' => $answers[$q][$a]['text']))); ?>
		 	<?php echo($form->hidden('Answer.'.$q.'.'.$a.'.id', array('value' => $answers[$q][$a]['id']))); ?>
		</td>
		<td>
			<!-- Move Up -->
			<?php echo $html->link($html->image("icons/arrow_large_up.png"),
		 	  array('controller' => 'Answers', 'action' => 'moveUp', $answers[$q][$a]['id'], $questions[$q]['Question']['id'],
			  $survey_id, $answers[$q][$a]['viewOrder']),
		 	  array('escape' => false))?>
			<!-- Move Down -->
			<?php echo $html->link($html->image("icons/arrow_large_down.png"),
		 	  array('controller' => 'Answers', 'action' => 'moveDown', $answers[$q][$a]['id'], $questions[$q]['Question']['id'],
			  $survey_id, $answers[$q][$a]['viewOrder']),
		 	  array('escape' => false))?>
		  	<!-- Delete -->
			<?php if($a > 1) { ?>
		  	<?php echo $html->link($html->image("icons/remove.png"),
			 array('controller'=>'Answers', 'action'=>'delete', $answers[$q][$a]['id'], $survey_id), null,
			 'Are you sure you want to delete this answer?',
			 array('escape' => false)); ?>
			<?php } ?>
		</td>
	</tr>
	<?php } ?>
	<tr>
		<td colspan="2" style="padding:25px 0px 0px 25px;">
			<div style="border-top: solid 1px #777777;">&nbsp;</div>
		</td>
	</tr>
	<?php } ?>
	<!-- End questions loop -->
	<tr>
		<td class="rightcell" colspan="2">
			<?php echo $form->end('Save'); ?>
		</td>
	</tr>
</table>

<!-- Survey Type -->
<div class="header">Survey Type</div>

<div style="padding-left:10px">
<?php if(!$open) { ?>
	<?php echo $form->create('Survey', array('action' => 'change_type')); ?>
	<?php
		$options = array('Available'=>'Available', 'Private'=>'Private', 'Restricted'=>'Restricted');
	?>
	Open survey as type: <?php echo $form->select('SurveyType.type', $options, $survey_type, null, false); ?>
	<div style="height:5px;line-height:5px;">&nbsp;</div>
	<?php echo $form->input('id', array('type'=>'hidden')); ?>
	<?php echo $form->end('Open'); ?>
<?php } else { ?>
	<?php echo $form->create('Survey', array('action' => 'change_type')); ?>
	<?php echo $form->input('id', array('type'=>'hidden')); ?>
	<?php echo $form->input('SurveyType.type', array('type'=>'hidden','value'=>'Closed')); ?>
	<?php echo $form->end('Close Survey'); ?>
<?php } ?>
</div>